package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();//连接
        Channel c = con.createChannel();//通信通道

        //1.创建队列  2.创建交换记  3.绑定
        String queue = UUID.randomUUID().toString();//随机队列名
        //                      非持久          独占            自动删除
        c.queueDeclare(queue, false, true, true, null);
        c.exchangeDeclare("logs", "fanout");
        //  进行绑定  队列名       交换记名       第三个参数对于fanout交换记无效
        c.queueBind(queue, "logs", "");

        //添加回调
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody());
                System.out.println("收到："+msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        //从自己的队列正常接收消息
        c.basicConsume(queue,true,deliverCallback,cancelCallback);

    }


}
